/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hanserver.action.hancadpo.refresh;

import br.com.sales.hanserver.action.hancadpe.GetDataPersonGridAction;
import br.com.sales.hanserver.action.hancadpo.consts.HanCadPOServerConsts;
import br.com.sales.hanserver.dao.Dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Karine
 */
public class RefreshProductDataGrid {

    public List<Map> getItems(Map items) {
        List<Map> resultData = new LinkedList<>();
        try {
            StringBuffer sql;
            sql = new StringBuffer(HanCadPOServerConsts.GET_PRODUCTS);

            if (items != null) {
                if (items.get("DS_PRODUTO") != null
                        && !"".equals(items.get("DS_PRODUTO").toString())) {
                    sql.append(" and upper(ds_produto) like '%").append(items.get("DS_PRODUTO").toString().toUpperCase()).append("%' ");
                }

                if (!isNullOrEmpty(items.get("DT_INICIO"))
                        && !isNullOrEmpty(items.get("DT_FIM"))) {
                    sql.append(" and a.dt_registro between ").append(" STR_TO_DATE('").append(items.get("DT_INICIO")).append("','%d/%m/%Y %T')");
                    sql.append(" and STR_TO_DATE('").append(items.get("DT_FIM")).append("','%d/%m/%Y %T')");
                }

            }
            sql.append(" order 	by 1,ds_produto");
            PreparedStatement st = Dao.getConn().prepareStatement(sql.toString());

            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Map map = new LinkedHashMap();
                map.put("SEQ", rs.getInt("SEQ"));
                map.put("DT_REGISTRO", rs.getString("DT_REGISTRO"));
                map.put("DS_PRODUTO", rs.getString("DS_PRODUTO"));
                map.put("VL_VENDA", rs.getString("VL_VENDA"));
                map.put("QT_ESTOQUE", rs.getInt("QT_ESTOQUE"));
                map.put("FORNEC", rs.getString("FORNEC"));
                map.put("VL_COMPRA", rs.getString("VL_COMPRA"));
                map.put("VL_PROMOCIONAL", rs.getString("VL_PROMOCIONAL"));
                map.put("PR_MARGEM_LUCRO", rs.getString("PR_MARGEM_LUCRO"));
                map.put("QT_MAX_ESTOQUE", rs.getString("QT_MAX_ESTOQUE"));
                map.put("QT_MIN_ESTOQUE", rs.getString("QT_MIN_ESTOQUE"));
                map.put("DS_BREVE", rs.getString("DS_BREVE"));
                map.put("IE_ESTOQUE", rs.getString("IE_ESTOQUE"));
                map.put("NR_SEQ_PF", rs.getString("NR_SEQ_PF"));
                resultData.add(map);
            }

            return resultData;

        } catch (Exception ex) {
            Logger.getLogger(GetDataPersonGridAction.class.getName()).log(Level.SEVERE, "Error getDataGrid", ex);
        } finally {
            Dao.closeConn();
        }
        return resultData;
    }

    private boolean isNullOrEmpty(Object value) {
        if (value == null) {
            return true;
        }
        return value instanceof String
                && value.toString().isEmpty();

    }
}
